/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;


import controllers.entity.Paper;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author hoangnv
 */
public class PaperDAO {
    private static PreparedStatement prst;
    public List <Paper>  getAllPaperByMaCanBo(int MaCanBo){
        List <Paper> list = new ArrayList<>();
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return null;
        }
        try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("SELECT * FROM Paper WHERE MaCanBo = ?");
            prst.setInt(1, MaCanBo);
            ResultSet rs = prst.executeQuery();
            while (rs.next()) {
                int maCanBo = rs.getInt("MaCanBo");
                String maPaper = rs.getString("MaPaper");
                int so = rs.getInt("So");
                String tenTapChi  = rs.getString("TenTapChi");
                String thoiGian  = rs.getString("ThoiGian");
                float ISSN = rs.getFloat("ISSN");
                float IF = rs.getFloat("hIF");
                String tenBaiBao=rs.getString("TenBaiBao");
                Paper temp = new Paper(maPaper,maCanBo,tenTapChi,so,thoiGian,ISSN,IF,tenBaiBao);
                
                list.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        DBConnect.Close();
        return list;
    }
    public static List <Paper>  getAllPaper(){
        List <Paper> list = new ArrayList<>();
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return null;
        }
        try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("SELECT * FROM Paper");
            ResultSet rs = prst.executeQuery();
            while (rs.next()) {
                int maCanBo = rs.getInt("MaCanBo");
                String maPaper = rs.getString("MaPaper");
                int so = rs.getInt("So");
                String tenTapChi  = rs.getString("TenTapChi");
                String thoiGian  = rs.getString("ThoiGian");
                float ISSN = rs.getFloat("ISSN");
                float IF = rs.getFloat("hIF");
                String tenBaiBao=rs.getString("TenBaiBao");
                Paper temp = new Paper(maPaper,maCanBo,tenTapChi,so,thoiGian,ISSN,IF,tenBaiBao);
                
                list.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        DBConnect.Close();
        return list;
    }
    public int getCountPaper(){
        int count = 0;
        List <Paper> list = getAllPaper();
        for (Paper ch : list){
            Integer i = Integer.parseInt(ch.getMaPaper());
            if (i > count ) count = i;
        }
        return count;
    }
    public boolean InsertPaper(Paper X){
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return false;
        }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("INSERT INTO Paper"
                    + "(MaCanBo,MaPaper,So,TenTapChi,ThoiGian,ISSN,hIF,TenBaiBao)"
                    + "VALUES (?,?,?,?,?,?,?,?)");
            prst.setInt(1,X.getMaCanBo());
            prst.setString(2, X.getMaPaper());
            prst.setInt(3,X.getSo());
            prst.setString(4,X.getTenTapChi());
            prst.setString(5,X.getThoiGian());
            prst.setFloat(6,X.getISSN());
            prst.setFloat(7, X.getIF());
            prst.setString(8, X.getTenBaiBao());
            if (prst.executeUpdate() >0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
    public boolean UpdatePaper(Paper X){
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return false;
        }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("UPDATE Paper SET "
                    + "MaCanBo = ?,MaPaper = ?,TenTapChi = ?,So = ?,ThoiGian = ?,"
                    + "hIF = ?,ISSN = ?,TenBaiBao=? WHERE MaPaper = ?");
                                                                       
            prst.setInt(1,X.getMaCanBo());
            prst.setString(2, X.getMaPaper());
            prst.setString(3,X.getTenTapChi());
            prst.setInt(4,X.getSo());
            prst.setString(5,X.getThoiGian());
            prst.setFloat(6, X.getIF());
            prst.setFloat(7,X.getISSN());
            prst.setString(8,X.getTenBaiBao());
            prst.setString(9,X.getMaPaper());
            if (prst.executeUpdate() > 0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
    public boolean DeletePaperByMaPaper(String MaPaper){
       boolean ok = DBConnect.Connect();
       if (!ok) {
            DBConnect.Close();
            return false;
       }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("DELETE FROM Paper WHERE MaPaper = ? ");
            prst.setString(1,MaPaper);
            if (prst.executeUpdate() > 0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
    public boolean DeletePaperByMaCanBo(int MaCanBo){
       boolean ok = DBConnect.Connect();
       if (!ok) {
            DBConnect.Close();
            return false;
       }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("DELETE FROM Paper WHERE MaCanBo = ? ");
            prst.setInt(1,MaCanBo);
            if (prst.executeUpdate() > 0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
}
